<template>
  <div class="container">
    <div class="title">
      新品上市
    </div>
    <div class="content">
      <div class="hot">
        <div class="titlee">
          <div class="boxx">
            <ul>
              <li v-for="(item, index) in records" :key="index">
                <div class="image">
                  <img :src="item.imgUrl" alt="">
                </div>
                <div class="bottomm">
                  <div class="adaff">
                    <div class="cakeName">{{ item.cakeName }}</div>
                    <div class="cakePrice">￥{{ item.price / 100 }}</div>
                  </div>
                  <div class="buyIt">
                    <el-button type="primary" size="mini" @click="BuyHandle(item.id)">加入购物车</el-button>
                  </div>
                </div>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
    <div class="bottom">
      <el-pagination background layout="prev, pager, next,jumper" @current-change="currentChange" :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getCakeById ,addCake} from '@/api/index';
export default {
  name: 'newProduct',
  data() {
    return {
      pageNum: 1,
      pageSize: 8,
      records: [],
      total: 0
    }
  },
  methods: {
    BuyHandle(id) {
      let param = {
        cakeId: id,
        count: 1
      }
      addCake(param).then(res => {
        this.$message({
          type: 'success',
          message: '添加成功！'
        }).catch(err => {
          this.$message({
            type: 'error',
            message: '添加失败，请检查网络'
          })
        })
      })
    },
    getCakeHot() {
      getCakeById(this.pageNum, this.pageSize, 3).then(res => {
        this.records = res.data.records
        this.total = res.data.total
        this.pageNum = res.data.current
      })
    },
    currentChange(current) {
      this.pageNum = current
      this.getCakeHot()
    }
  },
  mounted() {
    this.getCakeHot()
  }
}
</script>

<style lang="less" scoped>
.container {
  width: 1200px;

  .title {
    padding-left: 10px;
    position: relative;
    font-size: 30px;
    font-weight: bold;
  }

  .title::before {
    position: absolute;
    top: 50%;
    left: 0;
    content: '';
    width: 6px;
    height: 80%;
    background-color: #409eff;
    border-radius: 10px;
    transform: translateY(-50%);
  }

  .hot {
    margin-top: 30px;

    .image img {
      width: 100%;
      height: 280px;
    }

    .titlee {
      margin-bottom: 20px;
    }

    .boxx ul {
      width: 100%;
      display: flex;
      flex-direction: row;
      flex-wrap: wrap;
      justify-content: space-between;

      li {
        &:hover {
          border: 2px solid #409eff;
        }

        border: 2px solid transparent;
        width: 23%;
        // margin: 1px;
        margin-top: 10px;
        margin-left: 3px;
        padding: 5px;
        overflow: hidden;

        .image {
          cursor: pointer;
          position: relative;

          .detailTips {
            // display: none;
            width: 100%;
            height: 50px;
            position: absolute;
            bottom: 50%;
            background: rgba(0, 0, 0, 0.5);
            line-height: 50px;
            font-size: 14px;
            margin-left: 1000px;
            transition: all .25s;

            .buyButton {
              margin-top: 10px;
              margin-right: 20px;
              float: right;
              display: flex;
              justify-content: center;
              align-items: center;
              color: #fff;
              border: 1px solid #fff;
              width: 100px;
              height: 30px;
            }
          }
        }

        .image:hover .detailTips {
          // display: block;
          margin-left: 0;
          cursor: pointer;
        }

        .bottomm {
          display: flex;
          justify-content: space-between;
          align-items: center;

          .buyIt {}
        }

        .cakeName {
          font-size: 16px;
          font-weight: bold;
          margin: 5px 0;
        }

        .cakePrice {
          color: red;
          font-size: 32px;
        }
      }
    }

  }

  .bottom {
    width: 100%;
    display: flex;
    justify-content: center;
    margin-bottom: 10px;
  }
}
</style>